package com.hyacinth.utils.bean;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * Bean实例化异常
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class BeanInstantiationException extends RuntimeException{

    private static final long serialVersionUID = 5026662752431166615L;

    private final Class<?> beanClass;
    private final Constructor<?> constructor;
    private final Method constructingMethod;

    public BeanInstantiationException(Class<?> beanClass, String msg) {
        this((Class)beanClass, msg, (Throwable)null);
    }

    public BeanInstantiationException(Class<?> beanClass, String msg, Throwable cause) {
        super("Failed to instantiate [" + beanClass.getName() + "]: " + msg, cause);
        this.beanClass = beanClass;
        this.constructor = null;
        this.constructingMethod = null;
    }

    public BeanInstantiationException(Constructor<?> constructor, String msg, Throwable cause) {
        super("Failed to instantiate [" + constructor.getDeclaringClass().getName() + "]: " + msg, cause);
        this.beanClass = constructor.getDeclaringClass();
        this.constructor = constructor;
        this.constructingMethod = null;
    }

    public BeanInstantiationException(Method constructingMethod, String msg, Throwable cause) {
        super("Failed to instantiate [" + constructingMethod.getReturnType().getName() + "]: " + msg, cause);
        this.beanClass = constructingMethod.getReturnType();
        this.constructor = null;
        this.constructingMethod = constructingMethod;
    }

    public Class<?> getBeanClass() {
        return this.beanClass;
    }

    public Constructor<?> getConstructor() {
        return this.constructor;
    }

    public Method getConstructingMethod() {
        return this.constructingMethod;
    }
}
